package com.songshuai.webapi.permission.service.impl;

import com.songshuai.webapi.baseapi.utils.RegexUtil;
import com.songshuai.webapi.common.redis.RedisUtil;
import com.songshuai.webapi.permission.dao.YxUserRolesMapper;
import com.songshuai.webapi.permission.dto.po.UserHasPermissionPojo;
import com.songshuai.webapi.permission.model.YxUsers;
import com.songshuai.webapi.permission.service.CurrentUserService;
import com.songshuai.webapi.permission.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Auther: Administrator
 * @Date: 2018/11/26 0026 13:23
 * @Description: 当前登录人信息
 */
@Component
@Slf4j
public class CurrentUserServiceImpl implements CurrentUserService {

    @Autowired
    private UsersService usersService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 判断当前登录人是否是系统管理员
     */
    @Value("${config.self.sysadmin}")
    private String sysAdmins;

    @Resource
    private YxUserRolesMapper userRolesMapper;


    /**
     * 获取当前登录用户的用户名
     * @return
     */
    @Override
    public String getUserName() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        String name= String.valueOf(principal);
        System.out.println(name);
        return  name;
    }

    /**
     * 获取当前用户ID
     * @return
     */
    @Override
    public Integer getUserId() {
        return  getUserBaseInfo().getId();
    }

    /**
     * 获取当前用户基本信息
     * @return
     */
    @Override
    public YxUsers getUserBaseInfo(){
        String username=this.getUserName();
        int integer = RegexUtil.validPhoneOrMail(username);
        YxUsers info = usersService.getUserInfoByUsername(integer, username);
        return info;
    }


/*    *//**
     * 获取当前用户权限信息
     * @return
     *//*
    @Override
    public UserHasPermissionPojo getUserPermissionInfo(){
        String username=this.getUserName();
        int integer = RegexUtil.validPhoneOrMail(username);
        UserHasPermissionPojo userPermissionInfo=null;
        Object o = redisUtil.get(username);
        if(null!=o){
            return (UserHasPermissionPojo)o;
        }
        userPermissionInfo = usersService.getUserPermissionInfo(integer, username);
        if(null!=userPermissionInfo){
            redisUtil.set(username,userPermissionInfo,24*3600L);
        }

        return userPermissionInfo;
    }*/

    /**
     * 判断当前登录人是否是超级管理员
     * @return
     */
    @Override
    public Boolean CheckIsSysAdmin(){
        Boolean retData=false;
        Integer currentUserId= getUserId();

        if(currentUserId==null){
            return retData;
        }
        if(sysAdmins==null||sysAdmins.length()==0){
            return retData;
        }
        String[] userIds=sysAdmins.split(",");
        for(String uid:userIds){
            if(uid.equalsIgnoreCase(currentUserId.toString())){
                //retData=true;
                //break;
                return true;
            }
        }
        if(userRolesMapper.checkIsSysAdmin(currentUserId)>0){
            return true;
        }
        return retData;
    }


}
